import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

class LogManager {
  static final Logger _logger = Logger('MyApp');
  static File? _logFile;

  static Future<void> init() async {
    final directory = await getApplicationDocumentsDirectory();
    _logFile = File('${directory.path}/app_logs.txt');

    Logger.root.level = Level.ALL;
    Logger.root.onRecord.listen((record) {
      final message = '${record.time}: ${record.level.name}: ${record.message}';
      print(message);
      _writeToFile(message);
    });
  }

  static void _writeToFile(String message) {
    _logFile?.writeAsStringSync('$message\n', mode: FileMode.append);
  }

  static void logError(dynamic error, StackTrace? stack) {
    _logger.severe('Error: $error', error, stack);
  }
  static void logInfo(String msg) {
    _logger.severe('Info: $msg');
  }
}